package com.exam.demo.controller;

import com.alibaba.fastjson.JSON;
import com.exam.demo.pojo.Problem;
import com.exam.demo.pojo.Test;
import com.exam.demo.service.ExamService;
import com.exam.demo.service.LibraryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/exam")
//用于生成试卷的类
public class    ExamController {
    @Autowired
    private ExamService examService;

    @Autowired
    private LibraryService libraryService;

    @RequestMapping("/create")
    @ResponseBody
    private int createTest(@RequestParam("tstName") String tstName,
                           @RequestParam("tstType") String tstType,
                           @RequestParam("tstDif") String tstDif,
                           @RequestParam("tstTotalNum") Integer tstNum,
                           @RequestParam("type1Num") Integer type1Num,
                           @RequestParam("type2Num") Integer type2Num,
                           @RequestParam("type3Num") Integer type3Num,
                           @RequestParam("tstScore") Float tstScore) {
        List<Integer> proNums = new ArrayList<>();
        proNums.add(type1Num);
        proNums.add(type2Num);
        proNums.add(type3Num);
        int proNumTotal = 0;
        for (int i = 0; i < 3; ++i) {
            proNumTotal += proNums.get(i);
        }
        if (proNumTotal != tstNum) {
            return 0;
        }

        Test test = new Test();
        test.setTstNo(null);
        test.setTstName(tstName);
        test.setTstType(tstType);
        test.setTstDif(tstDif);
        test.setTstScore(tstScore);
        test.setTstTime(new Date());

        List<Integer> testProblems = new ArrayList<>();
        String[] types = new String[]{"判断题", "问答题", "填空题"};
        for (int i = 0; i < 3; ++i) {
            if (proNums.get(i) == 0) {
                continue;
            }
            List<Problem> problems = libraryService.getProblemListByProDifAndProType(tstDif, types[i]);
            Collections.shuffle(problems);
            if (problems.size() >= proNums.get(i)) {
                List<Problem> randomSeries = problems.subList(0, proNums.get(i));
                for (Problem p : randomSeries) {
                    testProblems.add(p.getProNo());
                }
            } else {
                return 0;
            }
        }

        if(tstNum != testProblems.size()){
            return 0;
        }

        test.setTstNum(tstNum);
        test.setTstDetail(JSON.toJSONString(testProblems));
        System.out.println(JSON.toJSONString(testProblems));
        return examService.addTest(test);
    }

    @RequestMapping("/addtest")
    private @ResponseBody int addTest(@RequestBody Test test){
        test.setTstTime(new Date());
        return this.examService.addTest(test);
    }


    @RequestMapping("/delete")
    @ResponseBody
    private int deleteTest(@RequestParam("tstNo") Integer tstNo){
        return examService.deleteTest(tstNo);
    }

    @RequestMapping("/edit")
    @ResponseBody
    private int editTest(@RequestBody Test tst){
        return examService.editTest(tst);
    }

    @RequestMapping("/querybytstno")
    @ResponseBody
    private Test queryTest(@RequestParam("tstNo") Integer tstNo){
        return examService.queryTest(tstNo);
    }

    @RequestMapping("/querybytstname")
    @ResponseBody
    private List<Test> queryTestByName(@RequestParam("tstName") String name){
        return examService.queryTestByName(name);
    }

    @RequestMapping("/querybytsttime")
    @ResponseBody
    private List<Test> queryTestByTime(@RequestParam("tstTime1") Date date1, @RequestParam("tstTime2") Date date2) {
        return examService.queryTestByTime(date1, date2);
    }

    @RequestMapping("/querybytstdif")
    @ResponseBody
    private List<Test> queryTestByDif(@RequestParam("tstDif") String dif){
        return examService.queryTestByDif(dif);
    }

    @RequestMapping("/querybytsttype")
    @ResponseBody
    private List<Test> queryTestByType(@RequestParam("tstType") String type){
        return examService.queryTestByType(type);
    }

    @RequestMapping("/queryall")
    @ResponseBody
    private List<Test> queryTest(){
        return examService.queryAllTest();
    }
}
